package Lee_code;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author blh
 * @date 2024年08月07日 9:51
 */
public class Lee_946_87 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int len = pushed.length;
        Deque<Integer> deque = new ArrayDeque<>();
        int index = 0;
        for (int i = 0; i < popped.length; i++) {
            if (deque.isEmpty() || deque.peek() != pushed[i]) {
                if (index >= popped.length) {
                    return false;
                }
                deque.push(pushed[index++]);
            } else {
                deque.pop();
            }
        }
        return true;
    }
}
